package com.iwdnb.gkgz.infrastructure.dao.repo.impl;

import java.util.List;
import java.util.Objects;

import cn.hutool.core.date.DateUtil;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.common.model.dto.StockFenshiData;
import com.iwdnb.gkgz.common.utils.CandleDataBuilder;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockFenshiRepository;
import com.iwdnb.wwzy.dao.MetaDataDAO;
import com.iwdnb.wwzy.model.query.MetaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.iwdnb.gkgz.common.constant.GkgzConstants.OBJECT_CODE_STOCK_FENSHI;

@Component
public class StockFenshiRepositoryImpl implements StockFenshiRepository {
    @Autowired
    private MetaDataDAO metaDataDAO;

    @Override
    public StockFenshiData queryFenshiList(String code, String date) {
        MetaQuery metaQuery = new MetaQuery();
        metaQuery.addParam("code", code);
        metaQuery.addParam("date", date);
        StockFenshiData stockFenshiData = metaDataDAO.get(OBJECT_CODE_STOCK_FENSHI, metaQuery, StockFenshiData.class);
        return stockFenshiData;
    }

    @Override
    public void save(StockFenshiData stockFenshiData) {
        MetaQuery metaQuery = new MetaQuery();
        metaQuery.addParam("code", stockFenshiData.getCode());
        metaQuery.addParam("date", DateUtil.formatDate(stockFenshiData.getDate()));
        StockFenshiData old = metaDataDAO.get(OBJECT_CODE_STOCK_FENSHI, metaQuery, StockFenshiData.class);
        if (Objects.nonNull(old)) {
            stockFenshiData.setId(old.getId());
        }
        stockFenshiData.setDate(DateUtil.beginOfDay(stockFenshiData.getDate()));
        if (Objects.nonNull(old)) {
            metaDataDAO.update(OBJECT_CODE_STOCK_FENSHI, stockFenshiData);
        } else {
            metaDataDAO.insert(OBJECT_CODE_STOCK_FENSHI, stockFenshiData);
        }
    }

}
